package adapter;

/**
 * Created by xueal on 2018/1/21.
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jph.simple.R;

import java.util.List;

import model.Option;


public class OptionAdapter extends ArrayAdapter<Option> {    

    private int resourceId;

    public OptionAdapter(Context context, int textViewResourceId,
                         List<Option> objects) {                         
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;                   
    }

    @Override         
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        OptionViewHolder viewHolder;                  
        if (convertView == null) {
            viewHolder = new OptionViewHolder();
            view = LayoutInflater.from(getContext()).inflate(
                    resourceId, null);          
            viewHolder.ivImage = (ImageView) view.findViewById(R.id.ivImage);
            viewHolder.tvName = (TextView) view.findViewById(R.id.tvName);   

            view.setTag(viewHolder);
        } else {
            view = convertView;           
            viewHolder = (OptionViewHolder) view.getTag();
        }

        Option option = getItem(position);

        viewHolder.ivImage.setImageResource(option.getImageID());
        viewHolder.tvName.setText(option.getName());        
        
        return view;
    }
}

class OptionViewHolder {      
    ImageView ivImage;
    TextView tvName;
}
